package com.cotte.flow.hbase.dao;

import java.io.IOException;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cotte.flow.hbase.record.StepEmpRecord;
import com.cotte.flow.hbase.table.BrushCardStruct;
import com.cotte.flow.hbase.table.StepEmpStruct;

/**
 * 保存工序+刷卡人 和刷卡时间
* <p>Title: StepEmpDaoImpl</p>  
* <p>Description: </p>  
* @author YGJ  
* @date 2018年12月5日
 */
@Component
public class StepEmpDaoImpl {
	
	@Autowired
	private Connection connection;
	
	public void putRecord(StepEmpRecord stepEmpRecord) {
		Table table = null;
		try{
			table = connection.getTable(TableName.valueOf(StepEmpStruct.TABLE));
			Put put = new Put((stepEmpRecord.getStepcode_empcode()).getBytes());
			put.addColumn(StepEmpStruct.FAMILY.getBytes(), BrushCardStruct.COLUMN_LOADDATE.getBytes(), stepEmpRecord.getLoadDate().getBytes());
			table.put(put);
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				table.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取上一次刷卡时间
	 * @param odid
	 * @return
	 */
	public String getLoadDate(String stepemp){
		Table table = null;
		try {
			table = connection.getTable(TableName.valueOf(StepEmpStruct.TABLE));
			Get get = new Get(stepemp.getBytes());
			Result result = table.get(get);
			byte[] value = result.getValue(StepEmpStruct.FAMILY.getBytes(),StepEmpStruct.COLUMN_LOADDATE.getBytes());
			if(value != null){
				return new String(value);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				table.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}